// circle/mine/mine.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    guanzhu: 0,
    fensi: 0,
    loginCircle: 0,
    ishow1: true,
    ishow2: false,
    avatarUrl: '',
    username: '',
    openId: '',
    openId2: '',
    everyDate: ''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {

    // 判断签到的时间
    wx.getStorage({
      key: 'every-date',
      success: (res) => {
        this.setData({
          everyDate: res.data
        })
      },
    })
    var ssDate = new Date().getDate()
    if (this.data.everyDate != ssDate) {
      this.setData({
        ishow1: false,
        ishow2: true
      })
    } else {
      return
    }

    var db = wx.cloud.database()

    wx.getStorage({
      key: 'openId',
      success: (res) => {
        console.log(res)
        this.setData({
          openId: res.data
        })
        db.collection('users').where({
          _openid: this.data.openId
        }).get({
          success: (res) => {
            console.log(res)
            this.setData({
              openId2: res.data[0]._openid
            })
            if (res.data == []) {
              this.setData({
                avatarUrl: res.data[0].photoUrl,
                username: res.data[0].username
              })
              var db = wx.cloud.database()
              db.collection('users').add({
                data: {
                  username: res.userInfo.nickName,
                  photoUrl: res.userInfo.avatarUrl,
                },
                success: res => {
                  console.log('00')
                },
              })
            } else {
              this.setData({
                avatarUrl: res.data[0].photoUrl,
                username: res.data[0].username
              })
            }
            // this.setData({
            //   avatarUrl: res.data[0].photoUrl,
            //   username: res.data[0].username
            // })
          }
        })
      },
    })




    // wx.getStorage({
    //   key: 'login-Message',
    //   success: function(res) {
    //     // this.setData({
    //     //   avatarUrl: res.userInfo.avatarUrl,
    //     //   username: res.userInfo.nickName
    //     // })
    //   },
    // })

    wx.getStorage({
      key: 'qd-Status',
      success: (res) => {
        if (res.data == true) {
          this.setData({
            ishow1: false,
            ishow2: true
          })
        }
      },
    })


  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {
   
  },
  qiandaoFun() {
    var aa = new Date()
    var getDate = new Date().getDate()
    wx.setStorage({
      key: 'every-date',
      data: getDate,
    })
    this.setData({
      ishow1: false,
      ishow2: true
    })

    wx.setStorage({
      key: 'qd-Status',
      data: this.data.ishow2,

    })
  },
  userDetail() {
    wx.navigateTo({
      url: '../zhuye/zhuye'
    })
  },
  shareFun() {
    wx.navigateTo({
      url: '../guanzhu/guanzhu',
    })
  },
  shareFun2() {
    wx.navigateTo({
      url: '../fensi/fensi',
    })
  },
  shareFun3() {
    wx.navigateTo({
      url: '../quanzi/quanzi',
    })
  },
  myCollectFun() {
    wx.navigateTo({
      url: '../shoucang/shoucang',
    })
  },
  myQzFun() {
    wx.navigateTo({
      url: '../mineQz/mineQz',
    })
  },
  myGiftFun() {
    wx.navigateTo({
      url: '../mineGift/mineGift',
    })
  },
  mineWallet() {
    wx.navigateTo({
      url: '../mineWallet/mineWallet',
    })
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function(e) {
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {
    
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})